package com.csdj.t309.interceptors;



import com.csdj.t309.utils.CookiesUtils;
import com.csdj.t309.utils.RedisUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Logininterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       Cookie token= CookiesUtils.getCookieByName(request,"token");
        Cookie id= CookiesUtils.getCookieByName(request,"id");
        Jedis j= RedisUtils.lianjie();
        boolean flag=true;
        if(token!=null){
            System.out.println("有cooke");
            String rtoken=j.hget("token","usertoken"+id.getValue());
            if(!rtoken.equals(token.getValue())){
                flag=false;
            }
        }else {
            flag=false;
        }
        if(!flag){
            System.out.println("lanjie");
            response.sendRedirect("/Maven_jiaxiao_war_exploded/login.html");
        }
        return flag;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
